1 using UnityEngine;
2 using
System.Collections;
3
4 public
class BallScript : MonoBehaviour
5 {
6
7     
public float SpeedY = 7;
8     
private Vector2 InitialLocation;
9
10     
// Use this for initialization
11     
void Start()
12     {
13         InitialLocation = transform.position;
14         GetComponent<Rigidbody2D>().velocity = Vector2.zero;
15     }
16
17     
// Update is called once per frame
18     
void Update()
19     {
20         
if (GameManager.CurrentGameState == GameManager.GameState.Playing)
21             GiveBoostIfMovingOnXorYAxis();
22     }
23
24     
private void GiveBoostIfMovingOnXorYAxis()
25     {
26         
if (Mathf.Abs(GetComponent<Rigidbody2D>().velocity.x - 0.2f) <= 0.2f)
27         {
28             
//left or right?
29             
bool right = Random.Range(-1.0f, 1.0f) >= 0;
30             GetComponent<Rigidbody2D>().AddForce(
new Vector2(right ? 5.0f : -5.0f, 0), ForceMode2D.Impulse);
31         }
32
33         
if (Mathf.Abs(GetComponent<Rigidbody2D>().velocity.y - 0.2f) <= 0.2f)
34         {
35             
//up or down?
36             
bool down = Random.Range(-1.0f, 1.0f) >= 0;
37             GetComponent<Rigidbody2D>().AddForce(
new Vector2(0, down ? 5.0f : -5.0f), ForceMode2D.Impulse);
38         }
39     }
40
41     
public void StartBall()
42     {
43         transform.position = InitialLocation;
44         GetComponent<Rigidbody2D>().velocity =
new Vector2(Random.Range(-3.0f, 3.0f), SpeedY);
45     }
46
47     
public void StopBall()
48     {
49         GetComponent<Rigidbody2D>().velocity = Vector2.zero;
50     }
51
52
53 }


Use this for initialization

Update is called once per frame

left or right?

up or down?




Trò chơi xếp gạch đơn giản 7.086 lượt xem

Gõ tìm kiếm nhanh...